package com.example.shopping.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.shopping.me.UserBean;

import java.util.Map;

public class SaveUserUtils {
    private  static UserBean.DataBean userinfo;
    public  static  UserBean.DataBean getUserinfo(){
        if (userinfo==null){

        }
        return userinfo;
    }
    public  static void  setUserinfo( UserBean.DataBean info ){
        if (info!=null){
            SaveUserUtils.userinfo=info;
        }
    }
    public static  void delUserInfo(){
        userinfo=null;
    }

    public static UserBean.DataBean getUserToObj(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        int id = preferences.getInt("id", -1);
        if (id != -1) {
            String phone = preferences.getString("phone", "");
            String password = preferences.getString("password", "");
            String name = preferences.getString("name", "");
            String adress = preferences.getString("adress", "");
            String nickname = preferences.getString("nickname", "");
            String question = preferences.getString("question", "");
            String answer = preferences.getString("answer", "");
            UserBean.DataBean bean = new UserBean.DataBean(id, phone, password, name, adress, nickname, question, answer);
            setUserinfo(bean);
            return bean;
        }
        return null;
    }
    //保存用户信息到本地文件的方法
    public static  void  saveUserInfo(Context context,UserBean.DataBean bean){
        SharedPreferences preferences= context.getSharedPreferences("user",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putInt("id",bean.getId());
        editor.putString("phone",bean.getPhone());
        editor.putString("password",bean.getPassword());
        editor.putString("name",bean.getName());
        editor.putString("address",bean.getAddress());
        editor.putString("nickname",bean.getNickname());
        editor.putString("question",bean.getQuestion());
        editor.putString("answer",bean.getAnswer());
        editor.commit();
        setUserinfo(bean);


    }
    //删除个人信息
    public static  void  deleteUserInfo(Context context,UserBean.DataBean bean){
        SharedPreferences preferences= context.getSharedPreferences("user",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.remove("id");
        editor.remove("phone");
        editor.remove("password");
        editor.remove("name");
        editor.remove("address");
        editor.remove("nickname");
        editor.remove("question");
        editor.remove("answer");
        editor.commit();
    }
}
